//
//  LargeGoodsDetailsViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/31.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "LargeGoodsDetailsViewController.h"
#import "PlatformCell.h"
#import "ReportPriceView.h"
#import "DeliveryRecordViewController.h"
#import "StateRecordViewController.h"
#import "BulkorderCell.h"
#import "AddressModel.h"
#import "ProductDetailModel.h"

@interface LargeGoodsDetailsViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) AddressModel *addressModel;
@property (nonatomic, strong) BulkorderModel *model;
@property (nonatomic, strong) ProductDetailModel *detailModel;

@end

@implementation LargeGoodsDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"大货单详情";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"collection"] style:UIBarButtonItemStyleBordered target:self action:@selector(collectionProduct)];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIUtil colorWithHexString:appFontColor]}];
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"bulkOrderId":self.bulkorderModel.bulkOrderId==nil?@"":self.bulkorderModel.bulkOrderId};
    [RequestData bulkorderDetailParameters:[UIUtil toJsonString:dic] withBlock:^(BulkorderModel *model, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.model = model;
            self.array = model.numList;
        }
    }];
}

- (void)setModel:(BulkorderModel *)model {
    _model = model;
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"productId":self.model.productId==nil?@"":self.model.productId};
    [RequestData getProductDetail:[UIUtil toJsonString:dic] withBlock:^(ProductDetailModel *model, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.detailModel = model;
            NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken,@"addressId":_model.userReceiveAddress==nil?@"":_model.userReceiveAddress};
            [RequestData queryAddressList:[UIUtil toJsonString:dic] withBlock:^(NSArray *array, NSString *msg, NSError *error) {
                if (error) {
                    if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                        [UIUtil tokenOverdue:self msg:msg];
                    } else {
                        [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
                        [self.navigationController popViewControllerAnimated:YES];
                    }
                } else {
                    _addressModel = array[0];
                }
            }];
            [self.myTableView reloadData];
        }
    }];
}

#pragma mark - action methods
- (IBAction)stateRecord:(id)sender {
    DeliveryRecordViewController *view = [[DeliveryRecordViewController alloc] init];
    view.model = self.model;
    view.ifBulkorder = YES;
    [self.navigationController pushViewController:view animated:YES];
}

- (IBAction)deliveryrecord:(id)sender {
    StateRecordViewController *view = [[StateRecordViewController alloc] init];
    view.model = self.model;
    [self.navigationController pushViewController:view animated:YES];
}

- (void)collectionProduct {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"productId":_model.productId==nil?@"":_model.productId,@"remark":@"",@"type":@"1"};
    [RequestData productCollect:[UIUtil toJsonString:dic] withBlock:^(NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"收藏失败":msg dismissBlock:nil];
            }
        } else {
            [[ESHUDView sharedInstance] showSuccessMessage:msg == nil?@"收藏成功":msg dismissBlock:nil];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *PlatformCellIdentifier = @"PlatformCell";
        [self.myTableView registerNib:[UINib nibWithNibName:@"PlatformCell" bundle:nil]
               forCellReuseIdentifier:PlatformCellIdentifier];
        PlatformCell *cell = [tableView dequeueReusableCellWithIdentifier:PlatformCellIdentifier];
        if (cell == nil) {
            cell = [[PlatformCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlatformCellIdentifier];
        }
        cell.model = self.detailModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 1){
            if (self.array) {
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 8, SCREEN_WIDTH - 24, (self.array.count + 1) * 30 - self.array.count - 1)];
                view.layer.masksToBounds = YES;
                view.layer.cornerRadius = 6.0;
                view.layer.borderWidth = 1;
                view.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
                for (int i = 0; i < self.array.count + 1; i++) {
                    if (i == 0) {
                        ReportPriceView *countView = [[ReportPriceView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 30)];
                        countView.array = @[@"颜色",@"尺寸",@"数量"];
                        [view addSubview:countView];
                    } else  {
                        ReportPriceView *countView = [[ReportPriceView alloc] initWithFrame:CGRectMake(0, 30 * i-i-1, view.frame.size.width, 30)];
                        countView.array = [self.array objectAtIndex:i-1];
                        countView.layer.borderWidth = 1;
                        countView.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
                        [view addSubview:countView];
                    }
                }
                [cell addSubview:view];
            }
        } else {
            static NSString *BulkorderCellIdentifier = @"BulkorderCell";
            [self.myTableView registerNib:[UINib nibWithNibName:@"BulkorderCell" bundle:nil]
                   forCellReuseIdentifier:BulkorderCellIdentifier];
            BulkorderCell *cell = [tableView dequeueReusableCellWithIdentifier:BulkorderCellIdentifier];
            if (cell == nil) {
                cell = [[BulkorderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:BulkorderCellIdentifier];
            }
            cell.model = self.model;
            cell.addressModel = self.addressModel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        return cell;
    }
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 92 + SCREEN_WIDTH * 53 / 64;
    } else if (indexPath.row == 1) {
        return (self.array.count + 1)*30 - self.array.count - 1 + 8;
    } else {
        return 190;
    }
}

@end
